// DlgSpecCentrifugalPump.cpp : implementation file
//

#include "stdafx.h"
#include "pdarx.h"
#include "DlgSpecCentrifugalPump.h"
#include "uesoftshare\user.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgSpecCentrifugalPump dialog


CDlgSpecCentrifugalPump::CDlgSpecCentrifugalPump ( CUESpecCentrifugalPump* pPump, CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSpecCentrifugalPump::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSpecCentrifugalPump)
	m_dA = 0.0;
	m_dB = 0.0;
	m_dC = 0.0;
	m_dD = 0.0;
	m_dE = 0.0;
	m_strEquipName = _T("");
	m_dF = 0.0;
	m_dG = 0.0;
	m_dH = 0.0;
	m_dI = 0.0;
	m_dJ = 0.0;
	m_dK = 0.0;
	m_dL = 0.0;
	m_strPrompt = _T("");
	m_strUnit = _T("");
	//}}AFX_DATA_INIT
	m_pPump = pPump;
	m_rsPtr.CreateInstance ( __uuidof ( Recordset ) );

	::AfxSetResourceHandle(g_ARXInstance);
}

CDlgSpecCentrifugalPump::~CDlgSpecCentrifugalPump()
{
}

void CDlgSpecCentrifugalPump::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSpecCentrifugalPump)
	DDX_Control(pDX, IDC_COMBO_NOM, m_cbNom);
	DDX_Control(pDX, IDC_COMBO_SPEC, m_cbSpec);
	DDX_Text(pDX, IDC_EDIT_A, m_dA);
	DDX_Text(pDX, IDC_EDIT_B, m_dB);
	DDX_Text(pDX, IDC_EDIT_C, m_dC);
	DDX_Text(pDX, IDC_EDIT_D, m_dD);
	DDX_Text(pDX, IDC_EDIT_E, m_dE);
	DDX_Text(pDX, IDC_EDIT_EQUIP_NAME, m_strEquipName);
	DDX_Text(pDX, IDC_EDIT_F, m_dF);
	DDX_Text(pDX, IDC_EDIT_G, m_dG);
	DDX_Text(pDX, IDC_EDIT_H, m_dH);
	DDX_Text(pDX, IDC_EDIT_I, m_dI);
	DDX_Text(pDX, IDC_EDIT_J, m_dJ);
	DDX_Text(pDX, IDC_EDIT_K, m_dK);
	DDX_Text(pDX, IDC_EDIT_L, m_dL);
	DDX_Text(pDX, IDC_EDIT_PROMT, m_strPrompt);
	DDX_Text(pDX, IDC_EDIT_UNIT, m_strUnit);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSpecCentrifugalPump, CDialog)
	//{{AFX_MSG_MAP(CDlgSpecCentrifugalPump)
	ON_CBN_SELCHANGE(IDC_COMBO_SPEC, OnSelchangeComboSpec)
	ON_CBN_SELCHANGE(IDC_COMBO_NOM, OnSelchangeComboNom)
	ON_EN_SETFOCUS(IDC_EDIT_A, OnSetfocusEditA)
	ON_EN_SETFOCUS(IDC_EDIT_B, OnSetfocusEditB)
	ON_EN_SETFOCUS(IDC_EDIT_EQUIP_NAME, OnSetfocusEditEquipName)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSpecCentrifugalPump message handlers

BOOL CDlgSpecCentrifugalPump::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	if ( m_pPump == NULL )
		return FALSE;

//	_RecordsetPtr m_rsPtr;
//	m_rsPtr.CreateInstance ( __uuidof ( Recordset ) );
	CString strQuery = _T ( "" );
	strQuery = _T ( "SELECT SPEC FROM PUMPA GROUP BY SPEC" );
	try
	{
	m_rsPtr->Open ( (_variant_t)strQuery, (IUnknown*)g_pConPump, adOpenStatic, adLockOptimistic, adCmdText );
	if ( m_rsPtr->adoEOF && m_rsPtr->BOF )
	{
		m_rsPtr->Close();
		return FALSE;
	}
	
	CString strFirstSpec = vtos ( m_rsPtr->GetCollect ( _T ( "SPEC" ) ) );
	m_rsPtr->MoveFirst();
	while ( !m_rsPtr->adoEOF )
	{
		m_cbSpec.AddString ( vtos ( m_rsPtr->GetCollect ( _T ( "SPEC" ) ) ) );
		m_rsPtr->MoveNext();
	}
	m_rsPtr->Close();

	strQuery.Format ( _T ( "SELECT CATALOG_NO FROM PUMPA WHERE SPEC = \'%s\'" ), strFirstSpec );
	m_rsPtr->Open ( (_variant_t)strQuery, (IUnknown*)g_pConPump, adOpenStatic, adLockOptimistic, adCmdText );
	if ( m_rsPtr->adoEOF && m_rsPtr->BOF )
	{
		m_rsPtr->Close();
		return FALSE;
	}
	CString strFirstNom = vtos ( m_rsPtr->GetCollect ( _T ( "CATALOG_NO" ) ) );
	while ( !m_rsPtr->adoEOF )
	{
		m_cbNom.AddString ( vtos ( m_rsPtr->GetCollect ( _T ( "CATALOG_NO" ) ) ) );
		m_rsPtr->MoveNext();
	}
	m_rsPtr->Close();

	strQuery.Format ( _T ( "SELECT * FROM PUMPA WHERE SPEC = \'%s\' AND CATALOG_NO = \'%s\'" ), strFirstSpec, strFirstNom );
	m_rsPtr.CreateInstance ( __uuidof ( Recordset ) );

	m_rsPtr->Open ( (_variant_t)strQuery, (IUnknown*)g_pConPump, adOpenStatic, adLockOptimistic, adCmdText );
	if ( m_rsPtr->adoEOF && m_rsPtr->BOF )
	{
		m_rsPtr->Close();
		return FALSE;
	}
	this->m_dA = vtof ( m_rsPtr->GetCollect ( _T ( "PARMA" ) ) );
	this->m_dB = vtof ( m_rsPtr->GetCollect ( _T ( "PARMB" ) ) );
	this->m_dC = vtof ( m_rsPtr->GetCollect ( _T ( "PARMC" ) ) );
	this->m_dD = vtof ( m_rsPtr->GetCollect ( _T ( "PARMD" ) ) );
	this->m_dE = vtof ( m_rsPtr->GetCollect ( _T ( "PARME" ) ) );
	this->m_dF = vtof ( m_rsPtr->GetCollect ( _T ( "PARMF" ) ) );
	this->m_dG = vtof ( m_rsPtr->GetCollect ( _T ( "PARMG" ) ) );
	this->m_dH = vtof ( m_rsPtr->GetCollect ( _T ( "PARMH" ) ) );
	this->m_dI = vtof ( m_rsPtr->GetCollect ( _T ( "PARMI" ) ) );
	this->m_dJ = vtof ( m_rsPtr->GetCollect ( _T ( "PARMJ" ) ) );
	this->m_dK = vtof ( m_rsPtr->GetCollect ( _T ( "PARMD" ) ) );
	this->m_dL = vtof ( m_rsPtr->GetCollect ( _T ( "PARML" ) ) );

	m_rsPtr->Close();
	}
	catch ( _com_error& e )
	{
		AfxMessageBox ( e.ErrorMessage() );
		return FALSE;
	}

	this->m_cbSpec.SetCurSel ( 0 );
	this->m_cbNom.SetCurSel ( 0 );

	this->UpdateData ( FALSE );
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSpecCentrifugalPump::OnSelchangeComboSpec() 
{
	// TODO: Add your control notification handler code here
	CString strQuery = _T ( "" );
	CString strSpec = _T ( "" );
	CString strNom = _T ( "" );

	for ( int i = m_cbNom.GetCount() - 1; i >= 0 ; i-- )
	{
		m_cbNom.DeleteString ( i );
	}

	m_cbSpec.GetLBText ( m_cbSpec.GetCurSel(), strSpec );
	strQuery.Format ( _T ( "SELECT CATALOG_NO FROM PUMPA WHERE SPEC = \'%s\'" ), strSpec );

	try
	{
	m_rsPtr->Open ( (_variant_t)strQuery, (IUnknown*)g_pConPump, adOpenStatic, adLockOptimistic, adCmdText );
	if ( m_rsPtr->adoEOF && m_rsPtr->BOF )
	{
		m_rsPtr->Close();
		return ;
	}

	while ( !m_rsPtr->adoEOF )
	{
		m_cbNom.AddString ( vtos ( m_rsPtr->GetCollect ( _T ( "CATALOG_NO" ) ) ) );
		m_rsPtr->MoveNext();
	}
	m_rsPtr->Close();
	this->m_cbNom.SetCurSel ( 0 );

	m_cbNom.GetLBText ( m_cbNom.GetCurSel(), strNom );
	strQuery.Format ( _T ( "SELECT * FROM PUMPA WHERE SPEC = \'%s\' AND CATALOG_NO = \'%s\'" ), strSpec, strNom );

	m_rsPtr->Open ( (_variant_t)strQuery, (IUnknown*)g_pConPump, adOpenStatic, adLockOptimistic, adCmdText );
	if ( m_rsPtr->adoEOF && m_rsPtr->BOF )
	{
		m_rsPtr->Close();
		return ;
	}
	this->m_dA = vtof ( m_rsPtr->GetCollect ( _T ( "PARMA" ) ) );
	this->m_dB = vtof ( m_rsPtr->GetCollect ( _T ( "PARMB" ) ) );
	this->m_dC = vtof ( m_rsPtr->GetCollect ( _T ( "PARMC" ) ) );
	this->m_dD = vtof ( m_rsPtr->GetCollect ( _T ( "PARMD" ) ) );
	this->m_dE = vtof ( m_rsPtr->GetCollect ( _T ( "PARME" ) ) );
	this->m_dF = vtof ( m_rsPtr->GetCollect ( _T ( "PARMF" ) ) );
	this->m_dG = vtof ( m_rsPtr->GetCollect ( _T ( "PARMG" ) ) );
	this->m_dH = vtof ( m_rsPtr->GetCollect ( _T ( "PARMH" ) ) );
	this->m_dI = vtof ( m_rsPtr->GetCollect ( _T ( "PARMI" ) ) );
	this->m_dJ = vtof ( m_rsPtr->GetCollect ( _T ( "PARMJ" ) ) );
	this->m_dK = vtof ( m_rsPtr->GetCollect ( _T ( "PARMD" ) ) );
	this->m_dL = vtof ( m_rsPtr->GetCollect ( _T ( "PARML" ) ) );
	m_rsPtr->Close();
	}
	catch ( _com_error& e )
	{
		AfxMessageBox ( e.ErrorMessage() );
		return ;
	}

	this->UpdateData ( FALSE );		
}

void CDlgSpecCentrifugalPump::OnSelchangeComboNom() 
{
	// TODO: Add your control notification handler code here
	CString strQuery = _T ( "" );
	CString strSpec = _T ( "" );
	CString strNom = _T ( "" );

	m_cbSpec.GetLBText ( m_cbSpec.GetCurSel(), strSpec );
	m_cbNom.GetLBText ( m_cbNom.GetCurSel(), strNom );
	strQuery.Format ( _T ( "SELECT * FROM PUMPA WHERE SPEC = \'%s\' AND CATALOG_NO = \'%s\'" ), strSpec, strNom );

	try
	{
	m_rsPtr->Open ( (_variant_t)strQuery, (IUnknown*)g_pConPump, adOpenStatic, adLockOptimistic, adCmdText );
	if ( m_rsPtr->adoEOF && m_rsPtr->BOF )
	{
		m_rsPtr->Close();
		return ;
	}
	this->m_dA = vtof ( m_rsPtr->GetCollect ( _T ( "PARMA" ) ) );
	this->m_dB = vtof ( m_rsPtr->GetCollect ( _T ( "PARMB" ) ) );
	this->m_dC = vtof ( m_rsPtr->GetCollect ( _T ( "PARMC" ) ) );
	this->m_dD = vtof ( m_rsPtr->GetCollect ( _T ( "PARMD" ) ) );
	this->m_dE = vtof ( m_rsPtr->GetCollect ( _T ( "PARME" ) ) );
	this->m_dF = vtof ( m_rsPtr->GetCollect ( _T ( "PARMF" ) ) );
	this->m_dG = vtof ( m_rsPtr->GetCollect ( _T ( "PARMG" ) ) );
	this->m_dH = vtof ( m_rsPtr->GetCollect ( _T ( "PARMH" ) ) );
	this->m_dI = vtof ( m_rsPtr->GetCollect ( _T ( "PARMI" ) ) );
	this->m_dJ = vtof ( m_rsPtr->GetCollect ( _T ( "PARMJ" ) ) );
	this->m_dK = vtof ( m_rsPtr->GetCollect ( _T ( "PARMD" ) ) );
	this->m_dL = vtof ( m_rsPtr->GetCollect ( _T ( "PARML" ) ) );
	m_rsPtr->Close();
	}
	catch ( _com_error& e )
	{
		AfxMessageBox ( e.ErrorMessage() );
		return ;
	}

	this->UpdateData ( FALSE );	
}

void CDlgSpecCentrifugalPump::OnSetfocusEditA() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSpecCentrifugalPump::OnSetfocusEditB() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSpecCentrifugalPump::OnSetfocusEditEquipName() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgSpecCentrifugalPump::OnOK() 
{
	// TODO: Add extra validation here
	if ( m_pPump != NULL )
	{
		m_pPump->m_strName = m_strEquipName;
		m_pPump->m_dA = m_dA;
		m_pPump->m_dB = m_dB;
		m_pPump->m_dC = m_dC;
		m_pPump->m_dD = m_dD;
		m_pPump->m_dE = m_dE;
		m_pPump->m_dF = m_dF;
		m_pPump->m_dG = m_dG;
		m_pPump->m_dH = m_dH;
		m_pPump->m_dI = m_dI;
		m_pPump->m_dJ = m_dJ;
		m_pPump->m_dK = m_dK;
		m_pPump->m_dL = m_dL;
	}
	
	CDialog::OnOK();
}

int CDlgSpecCentrifugalPump::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	
	::AfxSetResourceHandle(g_CADInstance);
	return 0;
}
